package com.wang.sort;

import java.util.Stack;

/**
 * @Author: along
 * @Create: 2021/9/2
 * @Description:剑指offer30 包含min函数的栈
 */
public class MinStack {
        Stack<Integer> A, B;
        /** initialize your data structure here. */
        public MinStack() {
            A = new Stack<>();
            B = new Stack<>();//辅助栈 栈顶存放A中的最小元素
        }

        public void push(int x) {
                A.add(x);
                if (B.isEmpty() || B.peek() >= x) {
                        B.add(x);
                }
        }

        public void pop() {
                if (B.peek().equals(A.pop())) {
                        B.pop();
                }
        }

        public int top() {
                return A.peek();
        }

        public int min() {
                return B.peek();
        }

}

